<template>
  <div class="app-container">
    <basic-container v-show="showSearch">
      <common-search @handleQuery="handleQuery" @resetQuery="resetQuery">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" size="mini">
          <el-form-item label="单证编号" prop="billCode">
            <el-input v-model="queryParams.billCode" placeholder="请输入单证编号" clearable
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item label="批次号" prop="manifestId">
            <el-input v-model="queryParams.manifestId" placeholder="请输入批次号" clearable
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item label="审核状态" prop="auditStatus">
            <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
              <el-option v-for="dict in dict.type.manual_release_audit_status" :key="dict.value" :label="dict.label"
                        :value="dict.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="核销状态" prop="billStatus">
            <el-select v-model="queryParams.billStatus" placeholder="请选择核销状态" clearable>
              <el-option v-for="dict in dict.type.sys_write_off_status" :key="dict.value" :label="dict.label"
                        :value="dict.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="IC卡" prop="icCards">
            <el-input v-model="queryParams.icCards" placeholder="请输入IC卡" clearable
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item label="车牌" prop="veNos">
            <el-input v-model="queryParams.veNos" placeholder="请输入车牌" clearable
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker v-model="daterangeCreateTime" style="width: 240px"
                            value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-form>
      </common-search>
    </basic-container>

    <basic-container>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:manualReleaseHead:add']">新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:manualReleaseHead:edit']">修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:manualReleaseHead:remove']">删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-top" size="mini" :disabled="multiple" @click="handleDeclare"
                    v-hasPermi="['system:manualReleaseHead:declare']">申报
          </el-button>
        </el-col>

        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['system:manualReleaseHead:export']"
          >导出</el-button>
        </el-col>
        -->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="manualReleaseHeadList" @selection-change="handleSelectionChange"
              stripe border size="small" >
        <el-empty slot="empty" :image="require('@/assets/images/empty.png')" :image-size="260"></el-empty>
        <el-table-column type="selection" width="55" align="center" fixed="left" :selectable="selectable"/>
        <el-table-column label="单证编号" prop="billCode" min-width="240" show-tooltip-when-overflow/>
        <el-table-column label="批次号" prop="manifestId" width="140" show-tooltip-when-overflow/>
        <el-table-column label="场所名称" align="center" prop="agentName" width="140" show-tooltip-when-overflow/>
        <el-table-column label="单证类型" align="center" prop="billType" min-width="130">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bill_type_list" :value="scope.row.billType"/>
          </template>
        </el-table-column>
        <el-table-column label="业务类型" align="center" prop="businessType" min-width="140">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_bill_business_type" :value="scope.row.businessType"/>
          </template>
        </el-table-column>
        <el-table-column label="进出口" align="center" prop="iEFlag">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_i_e_flag" :value="scope.row.iEFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="贸易标识" align="center" prop="tradeMark" min-width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.customs_trade_mark" :value="scope.row.tradeMark"/>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="auditStatus" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.manual_release_audit_status" :value="scope.row.auditStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="核销状态" align="center" prop="billStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_write_off_status" :value="scope.row.billStatus"/>
          </template>
        </el-table-column>
        <!--  <el-table-column label="货物重量" align="center" prop="totalGoodsWt" />
          <el-table-column label="货物数量" align="center" prop="totalGoodsPack" /> -->
        <el-table-column label="IC卡" align="center" prop="icCards" min-width="140" show-overflow-tooltip/>
        <el-table-column label="车牌" align="center" prop="veNos" width="120" show-overflow-tooltip/>
        <el-table-column label="车辆数" align="center" prop="veNums"/>
        <el-table-column label="申请原因" align="center" prop="applyReason" min-width="200" show-overflow-tooltip>
          <template slot-scope="scope">
            <dict-tag :options="dict.type.manual_release_reason" :value="scope.row.applyReason"/>
          </template>
        </el-table-column>
        <el-table-column label="申请说明" align="center" prop="applyReasonNote" min-width="150" show-overflow-tooltip/>
        <!--      <el-table-column label="创建人" align="center" prop="createBy" />
              <el-table-column label="创建时间" align="center" prop="createTime" width="180">
              </el-table-column>-->
        <el-table-column label="申报人" align="center" prop="declareBy" width="120" show-tooltip-when-overflow/>
        <el-table-column label="申报时间" align="center" prop="declareTime" width="180"/>
        <el-table-column label="附件" align="center" prop="attachFile" width="120">
          <template slot-scope="scope">
            <div v-if="scope.row.attachFile != ''&&scope.row.attachFile !=null">
              <a :href="baseUrl+scope.row.attachFile" style="text-decoration: underline;" target="_blank">
                <el-button icon="el-icon-view" size="mini" type="primary">查看附件</el-button>
              </a>
            </div>
            <div v-else>
              未上传附件
            </div>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark"  width="140" show-tooltip-when-overflow/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="showDetail(scope.row)"
                      v-hasPermi="['system:manualReleaseHead:query']">详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>
    </basic-container>

    <!-- 添加或修改人工放行管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" size="small" :rules="rules" label-width="80px" :disabled="isViewDisabled">
        <el-row>
          <el-col :span="15">
            <el-form-item label="作业单号" prop="billCode">
              <el-input v-model.trim="form.billCode" placeholder="有作业单的输入作业单号可快速填充"
                        :disabled="editMode">
                         <el-button slot="append" @click="billCodeChange()" icon="el-icon-search">快速填充</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <span style="color: red; line-height: 36px; padding-left: 10px;">*有作业单的可快速填充</span>
          </el-col>
        </el-row>
          <div class="flex-form">
            <el-form-item label="批次号" prop="manifestId">
              <el-input v-model.trim="form.manifestId" placeholder="请输入批次号" />
            </el-form-item>
         <!-- <el-col :span="12">
            <el-form-item label="单证类型" prop="billType">
              <el-select v-model="form.billType" placeholder="请选择单证类型"  style="width: 100%">
                <el-option v-for="dict in dict.type.bill_type_list" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
            <el-form-item label="过卡场所" prop="fieldCode" >
              <el-select v-model="form.fieldCode" placeholder="请选择过卡场所"  style="width: 100%">
                <el-option v-for="item in startCodeOptions" :key="item.value" :value="item.value"
                           :label="item.label"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="货物重量" prop="totalGoodsWt">
              <el-input v-model.number="form.totalGoodsWt" type="number" placeholder="请输入货物重量" />
            </el-form-item>
            <el-form-item label="货物数量" prop="totalGoodsPack">
              <el-input v-model.number="form.totalGoodsPack" type="number" placeholder="请输入货物数量" />
            </el-form-item>
            <el-form-item label="业务类型" prop="businessType">
              <el-select v-model="form.businessType" placeholder="请选择业务类型"  style="width: 100%">
                <el-option v-for="dict in dict.type.sys_bill_business_type" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="进出口" prop="iEFlag">
              <el-select v-model="form.iEFlag" placeholder="请选择进出口"  style="width: 100%">
                <el-option v-for="dict in dict.type.sys_i_e_flag" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="贸易标识" prop="tradeMark">
              <el-select v-model="form.tradeMark" placeholder="请选择进出口"  style="width: 100%" @change="tradeMarkChange">
                <el-option v-for="dict in dict.type.customs_trade_mark" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="申请原因" prop="applyReason">
              <el-select v-model="form.applyReason" placeholder="请选择申请人工放行原因" style="width: 100%">
                <el-option v-for="dict in dict.type.manual_release_reason" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="申请说明" prop="applyReasonNote">
              <el-input v-model="form.applyReasonNote" type="textarea" placeholder="请输入申请人工放行说明"/>
            </el-form-item>
            <el-form-item label="附件" prop="attachFile">
              <fileUpload v-model="form.attachFile" :fileSize="fileSize" :fileType="fileType"/>
            </el-form-item>
          </div>
        <el-row>
          <el-col :span="24">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-plus" size="mini" @click="handleListAdd">新增
                </el-button>
              </el-col>
            </el-row>
            <el-table :data="manualReleaseListList" stripe border>
              <el-table-column label="车牌号" align="center" prop="veNo"/>
              <el-table-column label="IC卡号" align="center" prop="icCard"/>
              <el-table-column label="车辆位置" align="center" prop="veNoPosition">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.vehicle_pass_gate_statius_list" :value="scope.row.veNoPosition"/>
                </template>
              </el-table-column>
              <el-table-column label="申请进出场" align="center" prop="iEType">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.ve_apply_cross_type_list" :value="scope.row.iEType"/>
                </template>
              </el-table-column>
              <!-- <el-table-column label="过卡类型" align="center" prop="veCrossType">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.ve_gross_type_list" :value="scope.row.veCrossType"/>
                </template>
              </el-table-column> -->
              <el-table-column label="自重" align="center" prop="veSelfWeight"/>
              <el-table-column label="通道号" align="center" prop="channelCodes" min-width="100" show-overflow-tooltip/>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleListUpdate(scope.row, scope.$index)"
                  >修改
                  </el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleListDelete(scope.row, scope.$index)"
                  >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="manualReleaseList.total>0"
              :total="manualReleaseList.total"
              :page.sync="manualReleaseList.pageNum"
              :limit.sync="manualReleaseList.pageSize"
              @pagination="getCarList"
            />

          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!isViewDisabled">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改信息" :visible.sync="item.open" width="700px" append-to-body :close-on-click-modal="false">
      <el-form ref="item_form" :rules="item.rules" :model="item.form" label-width="100px" class="flex-form" size="small">
            <el-form-item label="车牌号" prop="veNo">
              <el-input v-model="item.form.veNo" :disabled="veEditMode" placeholder="请输入车牌号" />
            </el-form-item>
            <el-form-item label="IC卡号" prop="icCard">
              <el-input v-model="item.form.icCard" placeholder="请输入IC卡号" />
            </el-form-item>
            <el-form-item label="电子车牌" prop="rfid">
              <el-input v-model="item.form.rfid" placeholder="请输入电子车牌" />
            </el-form-item>
            <el-form-item label="自重" prop="veSelfWeight">
              <el-input type="number" min="0" v-model.number="item.form.veSelfWeight" placeholder="请输入称重" />
            </el-form-item>
            <el-form-item label="车辆位置" prop="veNoPosition">
              <el-select v-model="item.form.veNoPosition" placeholder="请输入车辆位置" style="width: 100%">
                <el-option v-for="dict in veLocationList" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
         <!-- <el-col :span="12">
            <el-form-item label="过卡状态" prop="veCrossType">
              <el-select v-model="item.form.veCrossType" placeholder="请选择过卡状态" >
                <el-option v-for="dict in dict.type.ve_gross_type_list" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
            <el-form-item label="申请进出场" prop="iEType">
              <el-select v-model="item.form.iEType" placeholder="请选择申请进出场" @change="ieTypeChange" style="width: 100%">
                <el-option v-for="dict in dict.type.ve_apply_cross_type_list" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="通道号" prop="channelCodes">
            <el-select v-model="item.form.channelCodes" clearable filterable multiple size="small" placeholder="请选择通道号" style="width: 100%">
              <el-option v-for="item in channelInfoList" :key="item.value" :value="item.value"
                         :label="item.label"></el-option>
            </el-select>
          </el-form-item>
        <el-row>
          <el-col :span="24">
            <span style="color: red;">注意:选择进场，就只能刷进场，选择出场，只可刷出场，选进出场，就可以刷进出场</span>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="!veEditMode" @click="itemAddSubmitForm">确 定</el-button>
        <el-button type="primary" v-if="veEditMode" @click="itemSubmitForm">确 定</el-button>
        <el-button @click="itemCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情dialog-->
    <el-dialog :visible.sync="detailOpen" title="人工放行管理详情" width="1100px" append-to-body :close-on-click-modal="false">
      <el-descriptions :column="2" border :contentStyle="CS">
        <el-descriptions-item>
          <template slot="label">
            单证编号
          </template>
          {{ form.billCode }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            批次号
          </template>
          {{ form.manifestId }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            区代码
          </template>
          {{ form.customsCode }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            场所代码
          </template>
          {{ form.agentCode }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            场所名称
          </template>
          {{ form.agentName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            单证类型
          </template>
          <dict-tag :options="dict.type.bill_type_list" :value="form.billType"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            业务类型
          </template>
          <dict-tag :options="dict.type.sys_bill_business_type" :value="form.businessType"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            进出口
          </template>
          <dict-tag :options="dict.type.sys_i_e_flag" :value="form.iEFlag"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            贸易标识
          </template>
          <dict-tag :options="dict.type.customs_trade_mark" :value="form.tradeMark"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            审核状态
          </template>
          <dict-tag :options="dict.type.manual_release_audit_status" :value="form.auditStatus"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            核销状态
          </template>
          <dict-tag :options="dict.type.sys_write_off_status" :value="form.billStatus"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            货物重量
          </template>
          {{ form.totalGoodsWt }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            货物数量
          </template>
          {{ form.totalGoodsPack }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            IC卡
          </template>
          {{ form.icCards }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            车牌
          </template>
          {{ form.veNos }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            车辆数
          </template>
          {{ form.veNums }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            申请原因
          </template>
          <dict-tag :options="dict.type.manual_release_reason" :value="form.applyReason"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            申请说明
          </template>
          {{ form.applyReasonNote }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            创建人
          </template>
          {{ form.createBy }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            创建时间
          </template>
          {{ form.createTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            申报人
          </template>
          {{ form.declareBy }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            申报时间
          </template>
          {{ form.declareTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            报文id
          </template>
          {{ form.msgId }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            附件
          </template>
          <div v-if="form.attachFile != ''&&form.attachFile !=null">
            <a :href="baseUrl+form.attachFile" style="text-decoration: underline;" target="_blank">
              <el-button icon="el-icon-view" size="mini" type="primary">查看附件</el-button>
            </a>
          </div>
          <div v-else>
            未上传附件
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            备注
          </template>
          {{ form.remark }}
        </el-descriptions-item>
      </el-descriptions>
      <el-table :data="manualReleaseListList" stripe border style="margin-top: 15px;">
        <el-table-column label="车牌号" align="center" prop="veNo"/>
        <el-table-column label="IC卡号" align="center" prop="icCard"/>
        <el-table-column label="电子车牌" align="center" prop="rfid"/>
        <el-table-column label="车辆位置" align="center" prop="veNoPosition">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.vehicle_pass_gate_statius_list" :value="scope.row.veNoPosition"/>
          </template>
        </el-table-column>
        <el-table-column label="申请进出场" align="center" prop="iEType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.ve_apply_cross_type_list" :value="scope.row.iEType"/>
          </template>
        </el-table-column>

        <!-- <el-table-column label="过卡类型" align="center" prop="veCrossType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.ve_gross_type_list" :value="scope.row.veCrossType"/>
          </template>
        </el-table-column> -->
        <el-table-column label="称重" align="center" prop="veSelfWeight"/>
        <el-table-column label="通道号" align="center" prop="channelCodes" min-width="100" show-overflow-tooltip/>
      </el-table>
      <pagination
        v-show="manualReleaseList.total>0"
        :total="manualReleaseList.total"
        :page.sync="manualReleaseList.pageNum"
        :limit.sync="manualReleaseList.pageSize"
        @pagination="getCarList"
      />
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="closeDetail">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addManualReleaseHead,
  declareManualReleaseHead,
  delManualReleaseHead,
  getManualReleaseHead,
  listManualReleaseHead,
  updateManualReleaseHead
} from "@/api/kmwlpts/manualReleaseHead";
import {getRemedyBills} from "@/api/kmwlpts/bills"
import {getVeGrossType} from "@/utils/veCrossType"
import {
  findZcField,
  selectList
} from "@/api/system/baseFieldInfo";
import {queryChannelInfoList} from "@/api/system/channelInfo";
export default {
  name: "ManualReleaseHead",
  dicts: ['bill_type_list','sys_manifest_bill_type','sys_manifest_business_type', 'sys_bill_business_type', 'manual_release_audit_status', 'sys_attach_file_status', 'sys_write_off_status', 've_gross_type_list', 've_apply_cross_type_list', 'manual_release_reason', 'sys_i_e_flag','vehicle_pass_gate_statius_list', 'customs_trade_mark'],
  data() {
    return {
      CS: {
        'text-align': 'left',  //文本居中
        'min-width': '30%',   //最小宽度
        'width': '35%',
        'word-break': 'break-all'  //过长时自动换行
      },
      baseUrl: process.env.VUE_APP_BASE_FILE_URL,
      fileSize: 1,
      fileType: ["pdf"],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      //多选选中的行
      selectionRow: [],
      startCodeOptions: [],
      // 非单个禁用
      single: true,
      //编辑模式
      editMode: false,
      // 编辑的下标
      editIndex: -1,
      // 非多个禁用
      multiple: true,
      //详情显示的时候，表单编辑禁用
      isViewDisabled: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人工放行管理表格数据
      manualReleaseHeadList: [],
      // 人工放行列表表格数据
      manualReleaseListList: [],
      //车辆位置枚举
      veLocationList:[{label:'未进场',value:'0',listClass:''},{label:'已进场',value:'1',listClass:''}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      channelInfoList: [],
      open: false,
      //显示详情
      detailOpen: false,
      //车辆编辑模式
      veEditMode: false,
      // 备注时间范围
      daterangeCreateTime: [],
      //车辆列表的分页
      manualReleaseList: {
        total: 0,
        pageNum: 1,
        pageSize: 5
      },
      //列表中的数据
      item: {
        open: false,
        form: {
          channelCodes: [],
        },
        rules: {
          veNoPosition: [{
            required: true,
            message: "车辆位置不能为空",
            trigger: "blur"
          }],
          veNo: [{
            required: true,
            message: "车牌号不能为空",
            trigger: "blur"
          }],
          iEType: [{
            required: true,
            message: "车辆进出场类型不能为空",
            trigger: "blur"
          }],
          veSelfWeight: [{
            required: true,
            message: "车辆自重不能为空",
            trigger: "blur"
          }],
          channelCodes: [{
            required: false,
            message: "通道号不能为空",
            trigger: "blur"
          }],
         },
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billCode: null,
        manifestId: null,
        auditStatus: null,
        billStatus: null,
        icCards: null,
        veNos: null,
        createTime: null,
        trafMode:'4'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        manifestId: [{
          required: true,
          message: "批次号不能为空",
          trigger: "blur"
        }],
        iEFlag: [{
          required: true,
          message: "进出口不能为空",
          trigger: "blur"
        }],
        applyReason: [{
          required: true,
          message: "申请原因不能为空",
          trigger: "blur"
        }],
        applyReasonNote: [{
          required: true,
          message: "申请说明不能为空",
          trigger: "blur"
        }],
        fieldCode:[{
          required: true,
          message: "过卡场所不能为空",
          trigger: "blur"
        }],
        businessType:[{
          required: true,
          message: "业务类型不能为空",
          trigger: "blur"
        }],
        totalGoodsPack:[{
          required: true,
          message: "货物数量不能为空",
          trigger: "blur"
        }],
        totalGoodsWt:[{
          required: true,
          message: "货物重量不能为空",
          trigger: "blur"
        }],
        tradeMark:[{
          required: true,
          message: "贸易标识不能为空",
          trigger: "blur"
        }],
        attachFile: [{
          required: true,
          message: "附件不能为空",
          trigger: "blur"
        }],
      }
    };
  },
  created() {
    this.getList();
    this.getChannelInfo();
  },
  methods: {
    /** 查询人工放行管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listManualReleaseHead(this.queryParams).then(response => {
        this.manualReleaseHeadList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getZcFieldInfo() {
      let obj = {fieldName: null, customCode: null};
      findZcField(obj).then(response => {
        this.startCodeOptions = response;
        if(1==this.startCodeOptions.length){
          this.form.fieldCode=this.startCodeOptions[0];
        }
      });
    },
    getCarList() {
      this.manualReleaseList.total = this.manualReleaseListList.length;
      this.manualReleaseList.pageNum = 1;
      this.manualReleaseList.pageSize = 5;
    },
    getChannelInfo() {
      let fieldCode = this.$store.state.user.fieldCodes;
      queryChannelInfoList(fieldCode).then(res => {
        this.channelInfoList = res;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billCode: null,
        manifestId: null,
        customsCode: null,
        agentCode: null,
        agentName: null,
        billType: null,
        trafMode:'4',
        businessType: null,
        iEFlag: null,
        auditStatus: null,
        billStatus: null,
        totalGoodsWt: 0,
        totalGoodsPack: 0,
        icCards: null,
        veNos: null,
        veNums: null,
        applyReason: null,
        applyReasonNote: null,
        createBy: null,
        createTime: null,
        declareBy: null,
        declareTime: null,
        msgId: null,
        attachFile: null,
        attachStatus: "0",
        remark: null,
        fieldCode: null,
        fieldName: null,
        tradeMark: null,
      };
      this.resetForm("form");
      this.manualReleaseListList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.selectionRow = selection;
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isViewDisabled = false;
      this.open = true;
      this.title = "添加人工放行管理";
      this.editMode = false;
      this.getZcFieldInfo();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.load(id);
      this.title = "修改人工放行管理";
      this.open = true;
      this.editMode = true;
      this.getZcFieldInfo();
    },
    tradeMarkChange(val) {
      if(val === '2') {
        this.item.rules.channelCodes[0].required = true;
      } else {
        this.item.rules.channelCodes[0].required = false;
      }
    },

    /**车辆新增*/
    handleListAdd(){
       this.itemReset();
       this.item.open = true;
       this.veEditMode=false;
    },
    /** 修改按钮操作 */
    handleListUpdate(row, index) {
      this.itemReset();
      const id = row.id;
      this.item.open = true;
      this.item.form = {...row};
      this.item.form.channelCodes = row.channelCodes.split(",");
      this.veEditMode=true;
      this.editIndex = index;
    },
    itemReset() {
      this.item.form = {}
    },
    itemCancel() {
      this.item.open = false;
      this.itemReset();
    },
    /**修改 提交按钮 */
    itemSubmitForm() {
      this.$refs["item_form"].validate(valid => {
        if (valid) {
          let obj = {...this.item.form}
          obj.channelCodes = this.item.form.channelCodes.join(",");
          this.manualReleaseListList.splice(this.editIndex, 1, obj);
          this.item.open = false;
          this.itemReset();
        }
      })
    },
    //新增
    itemAddSubmitForm(){
      this.$refs["item_form"].validate(valid => {
        if (valid) {
            let existVeNos= this.manualReleaseListList.filter(n=>n.veNo==this.item.form.veNo);
            if(existVeNos&&existVeNos.length>0){
              this.$modal.alertWarning("此车牌已存在");
              return;
            }
            let  newObj = {... this.item.form };
            newObj.channelCodes = this.item.form.channelCodes.join(",");
            this.manualReleaseListList.push(newObj);
            this.item.open = false;
            this.itemReset();
        }
      });
    },


    //关闭详情
    closeDetail() {
      this.detailOpen = false;
    },
    //显示详情
    showDetail(row, column, event) {
      this.reset();
      const id = row.id;
      this.load(id);
      //打开详情
      this.detailOpen = true;
    },
    /** 加载数据 */
    load(id) {
      getManualReleaseHead(id).then(response => {
        this.form = response.data;
        this.manualReleaseListList = response.data.manualReleaseLists;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.manualReleaseListList.length == 0) {
            this.$modal.msgError("没有添加车辆信息！");
            return;
          }
          this.form.manualReleaseLists = this.manualReleaseListList;
          this.form.trafMode = '4';
          if (this.form.id != null) {
            updateManualReleaseHead(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.form.manualReleaseLists = [];
            });
          } else {
            addManualReleaseHead(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.form.manualReleaseLists = [];
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除人工放行管理编号为"' + ids + '"的数据项？').then(function () {
        return delManualReleaseHead(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    //申报
    handleDeclare() {
      let chkAttachFile = '1';
      this.selectionRow.forEach((item, i) => {
        if (item.attachStatus == '0') {
          chkAttachFile = item.billCode;
        }
      });
      if (chkAttachFile != '1') {
        this.$modal.msgError(chkAttachFile + "未上传附件，按照要求，卡口补录必须上传附件材料！");
        return;
      }
      const ids = this.ids;
      this.$modal.confirm('是否确认申报人工放行管理编号为"' + ids + '"的数据项？').then(function () {
        return declareManualReleaseHead(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("申报成功");
      }).catch(() => {
      });
    },

    //删除列表中的车辆
    handleListDelete(row, index) {
      this.manualReleaseListList.splice(index, 1)
    },
    /**查询单证信息*/
    billCodeChange() {
      let billCode = this.form.billCode;
      if (billCode == "" || billCode == null) {
        return false;
      }
      ;
      getRemedyBills(billCode).then(response => {
        if (response.data != null) {
          this.manualReleaseListList = [];
          this.reset();
          this.form.billCode = billCode;
          this.form.manifestId = response.data.transitCode;
          this.form.totalGoodsWt = response.data.totalGrossWt;
          this.form.totalGoodsPack = response.data.totalPackNo;
          this.form.businessType = response.data.businessType;
          this.form.billType = response.data.billType;
          this.form.iEFlag = response.data.iEFlag;
          if(response.data.startFieldCode){
            this.form.fieldCode=response.data.startFieldCode;
            this.form.fieldName=response.data.startFieldName;
          }else{
            this.form.fieldCode=response.data.agentCode;
            this.form.fieldName=response.data.agentName;
          }

          //manualReleaseListList
          if (response.data.gBillsIccardList != null) {
            response.data.gBillsIccardList.forEach((item, i) => {
              let isOff = true;
              let iEType = item.deInAreaFlag === '0' ? 'I' : 'E';
              let veNoPosition = item.deInAreaFlag;
              let level = '';
              if (item.deInAreaFlag == '0') {
                iEType = 'I';
                level = '1';
              } else if (item.deInAreaFlag == '1') {
                iEType = 'E';
                level = '2';
              } else if (item.deInAreaFlag == '2') {
                isOff = false;
              }
              // if(isOff){
              let myVeCrossType = getVeGrossType(this.form.businessType, level);
              let mlist = {
                seqNo: item.id,
                icCard: item.deIcCard,
                veNo: item.deTransName,
                transportId: item.transportId,
                iEType: iEType,
                veNoPosition: veNoPosition,
                vePosition: item.deInAreaFlag,
                veCrossType: myVeCrossType,
                veSelfWeight: item.deTransWeight
              };
              this.manualReleaseListList.push(mlist);
              //}
            });
            this.manualReleaseList.total = this.manualReleaseListList.length;
            this.manualReleaseList.pageNum = 1;
            this.manualReleaseList.pageSize = 5;

          }
        } else {
          this.$modal.msgError("此单证号未查询到相关数据！");
        }
        this.$forceUpdate();
      });
    },
    ieTypeChange(val) {
      if (this.item.form.vePosition == '1') {
        if (val == 'I' || val == 'IE') {
          this.$modal.msgError("车辆已进场");
          this.item.form.iEType = 'E';
        }
      } else if (this.item.form.vePosition == '0') {
        if (val == 'E') {
          this.$modal.msgError("车辆未进场");
          this.item.form.iEType = 'I';
        }
      }
    },
    //选择复选框控制哪些数据可以申报
    selectable(row, index) {
      let editStatus = ["1", "3", "10", "7"];
      if (editStatus.includes(row.auditStatus)) {
        return true;
      } else {
        return false;
      }
    },


  }
};
</script>
